var firstClassifyId;
var twoClassifyId;

init();
function init(){
    var classifyHtml = '<div class="height-a">'
            +'<div class="mail  mail-name font-weight"><p>分类名称</p></div>'
            +'<div class="mail  mail-name font-weight"><p>分类图片</p></div>'
            +'<div class="mail  mail-name font-weight"><p>分类时间</p></div>'
            +'<div class="mail  mail-name font-weight"><p>分类备注</p></div>'
            +'<div class="mail-right font-weight"><p>操作</p></div>'
            +'</div>';
    $.ajax({
        url	:	prefixUrl + "material/type/queryList",
        type:	"post",
        data:JSON.stringify({typeCode:"all"}),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var list = data.data;
            for(var i=0; i<list.length; i++){
                var typeDetail = list[i];
                if(typeDetail.typeCode.length ==4){
                    var firstDesc = typeDetail.typeDesc;
                    if(firstDesc == null || firstDesc == ""){
                        firstDesc = "无";
                    }
                    classifyHtml = classifyHtml + '<div class="height-a">'
                            +'<div class="mail mail-name"><a role="button" data-toggle="collapse" data-parent="#accordion" href="#type' + typeDetail.typeId + '" aria-expanded="true" aria-controls="collapsesix">'
                            +typeDetail.typeName
                            + '</a><i class="fa fa-angle-right  pull-right"></i></div>'
                            +'<div class="mail mail-name">无</div>'
                            +'<div class="mail mail-name"><p>' + new Date(typeDetail.oprDate).Format("yyyy-MM-dd") + '</p></div>'
                            +'<div class="mail mail-name"><p>' + firstDesc + '</p></div>'
                            +'<div class="mail-right">'
                            +'<button class="badge badge-success"  data-toggle="modal" data-target="#addTwoClassify" onclick=setFirstClassifyId("'
                            + typeDetail.typeId
                            + '") data-whatever="@mdo">添加</button>'
                            +'<button class="badge badge-warning"  data-toggle="modal" data-target="#updateFirstClassify" onclick=setFirstClassifyId("'
                            + typeDetail.typeId
                            + '") data-whatever="@mdo">修改</button>'
                            +'<button class="badge badge-danger" onclick=deleteFirstClassify("'
                            + typeDetail.typeId
                            + '")>删除</button>'
                            +'</div>'
                            +'</div>';
                    var isFirst = true;
                    for(var j=0; j<list.length; j++){
                        if(list[j].parentId == typeDetail.typeId){
                            if(isFirst){
                                classifyHtml = classifyHtml + '<div id="type' + list[j].parentId + '" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingsix">';
                            }
                            var twoDesc = list[j].typeDesc;
                            if(twoDesc == null || twoDesc == ""){
                                twoDesc = "无";
                            }
                            classifyHtml = classifyHtml
                                    +'<div class="height-b">'
                                    +'<div class="mail mail-name"><p>'
                                    +list[j].typeName
                                    +'</p></div>'
                                    +'<div class="mail  mail-name"><p>无</p></div>'
                                    +'<div class="mail  mail-name"><p>' + new Date(list[j].oprDate).Format("yyyy-MM-dd") + '</p></div>'
                                    +'<div class="mail  mail-name"><p>' + twoDesc + '</p></div>'
                                    +'<button class="badge badge-warning"  data-toggle="modal" data-target="#updateTwoClassify" onclick=setTwoClassifyId("'
                                    + list[j].typeId
                                    + '") data-whatever="@mdo">修改</button>'
                                    +'<button class="badge badge-danger" onclick=deleteTwoClassify("'
                                    + list[j].typeId
                                    + '")>删除</button>'
                                    +'</div>';
                            isFirst = false;
                        }
                    }
                    if(!isFirst){
                        classifyHtml = classifyHtml + '</div>';
                    }
                }
            }
            $("#classifyList").html(classifyHtml);
        }
    })

}

//一级分类Id赋值
function setFirstClassifyId(data){
    firstClassifyId = data;
    queryClassifyDetail(data,1);
}

//二级分类Id赋值
function setTwoClassifyId(data){
    twoClassifyId = data;
    queryClassifyDetail(data,2);
}

function queryClassifyDetail(id,type){
    $.ajax({
        url: prefixUrl + "material/type/queryDetail",
        type: "post",
        data: JSON.stringify({typeId:id}),
        contentType: "application/json;",
        success: function (data) {
            var classifyData = data.data;
            if(type == 1){
                $("#firstClassifyName").val(classifyData.typeName);
                $("#firstClassifyDesc").val(classifyData.typeDesc);
            }else{
                $("#twoClassifyName").val(classifyData.typeName);
                $("#twoClassifyDesc").val(classifyData.typeDesc);
            }
        }
    })
}


function addFirstClassify(data){
    var classifyName = $("#classifyNameFirst").val();
    var classifyDesc = $("#classifyDescFirst").val();

    addClassify(classifyName,classifyDesc,"",1);
}

function addTwoClassify(data){
    var classifyName = $("#classifyName").val();
    var classifyDesc = $("#classifyDesc").val();

    addClassify(classifyName,classifyDesc,firstClassifyId,2);
}

function addClassify(classifyName,classifyDesc,classificationParentId,classificationLevel){
    if(classifyName == null || classifyName == ""){
        alert("名称不可为空");
        return;
    }
    $.ajax({
        url: prefixUrl + "material/type/add",
        type: "post",
        data: JSON.stringify({
            typeName : classifyName,
            typeDesc : classifyDesc,
            parentId : classificationParentId
        }),
        contentType: "application/json;",
        success: function (data) {
            init();
            alert(data.msg);
            $(".modal-backdrop").css("display","none");
            $(".modal").css("display","none");
        }
    })
}

function updateFirstClassify(data){
    var classifyName = $("#firstClassifyName").val();
    var classifyDesc = $("#firstClassifyDesc").val();

    updateClassify(data,classifyName,classifyDesc,1);
}

function updateTwoClassify(data){
    var classifyName = $("#twoClassifyName").val();
    var classifyDesc = $("#twoClassifyDesc").val();

    updateClassify(data,classifyName,classifyDesc,2);
}


function updateClassify(id,classifyName,classifyDesc,type){
    if(classifyName == null || classifyName == ""){
        alert("名称不可为空");
        return;
    }
    if(type == 1){
        id = firstClassifyId;
    }else{
        id = twoClassifyId;
    }
    $.ajax({
        url: prefixUrl + "material/type/update",
        type: "post",
        data: JSON.stringify({
            typeId : id,
            typeName : classifyName,
            typeDesc : classifyDesc,
        }),
        contentType: "application/json;",
        success: function (data) {
            init();
            alert(data.msg);
            $(".modal-backdrop").css("display","none");
            $(".modal").css("display","none");
        }
    })
}

function deleteFirstClassify(data){
    if(document.getElementById(data)){
        alert("当前分类下还存在子节点，不可删除");
    }else{
        if(confirm("确定要进行删除嘛?")){
            //点击确定后操作
            deleteClassify(data);
        }
    }
}

function deleteTwoClassify(data){
    if(confirm("确定要进行删除嘛?")){
        //点击确定后操作
        deleteClassify(data);
    }
}

function deleteClassify(data){
    $.ajax({
        url: prefixUrl + "material/type/delete",
        type: "post",
        data: JSON.stringify({
            typeId : data
        }),
        contentType: "application/json;",
        success: function (data) {
            init();
            alert(data.msg);
        }
    })
}